/** 
 * 算出打吃的落子
 */

import {
  callback_board,
  isAvalablePos,
  callback_opponent_pos,
  get_all_position_of_chain,
  callback_surround_dot_4,
  is_pos_in_array,
  all_exit_of_chain,
} from "/go/common-utils.js"

var GNS={};

// function all_exit_of_chain(chain_number,chains,board_state)
// {
//   // 取链上所有的点:
//   let all_pos_of_chain = get_all_position_of_chain(chains,chain_number);
//   // 存这个链的出口列表
//   let arr_exit=[];
//   // 遍历分析链上所有点:
//   for(let pos_i =0;pos_i<all_pos_of_chain.length;++pos_i)
//   {
//     let pos = all_pos_of_chain[pos_i];
//     callback_surround_dot_4(pos,board_state,(x,y,value)=>{
//       if('.' == value)
//       {
//         let current_pos = [x,y];
//         if(!is_pos_in_array(arr_exit,current_pos) )
//         {
//           arr_exit.push(current_pos);
//         }
//       }
//     });
//   }// END for pos_i
//   // 结果,链有多少个出口:
//   // GNS.print("####    38:",arr_exit);
//   return arr_exit;
// }

export function figure_out_eat_drop(ns,board_state,chains,valid_moves) {
  GNS = ns;
  // 1.取所有的对手的链
  // 取对手所有的落子:
  let all_opponent_pos = callback_opponent_pos(board_state,()=>{});;
  //ns.print("#### 20",all_opponent_pos);
  // 取落子对应的链:
  let all_train_of_opponent=[];
  for(let i=0;i<all_opponent_pos.length;++i)
  {
    let opponent_pos = all_opponent_pos[i];
    let chain_number = chains[opponent_pos[0]][opponent_pos[1]];
    all_train_of_opponent.push( chain_number);
  }
  // callback_board(chains,(x,y,value)=>{
  //   all_train_of_opponent.push(value);
  // });//fun(x,y,board[x][y]);
  // 链去重
  all_train_of_opponent = all_train_of_opponent.filter((value, index, self) => {return self.indexOf(value) === index;});
  // 2. 遍历分析所有的对手的链
  // 存对手将死的链
  let arr_opponent_dying_chain = [];//{chain_number:*,chain_length:*,exit_pos:[x,y]}
  for(let o_c_i = 0;o_c_i<all_train_of_opponent.length;++o_c_i)
  {
    let opponent_chain_number = all_train_of_opponent[o_c_i];
    // BEGIN 取这个链的所有出口:
    // 取链上所有的点:
    let all_pos_of_chain = get_all_position_of_chain(chains,opponent_chain_number);
    let arr_exit=all_exit_of_chain(opponent_chain_number,chains,board_state);// 存这个链的出口列表
    // 遍历分析链上所有点:
    // for(let pos_i =0;pos_i<all_pos_of_chain.length;++pos_i)
    // {
    //   let pos = all_pos_of_chain[pos_i];
    //   callback_surround_dot_4(pos,board_state,(x,y,value)=>{
    //     if('.' == value)
    //     {
    //       let current_pos = [x,y];
    //       if(!is_pos_in_array(arr_exit,current_pos) )
    //       {
    //         arr_exit.push(current_pos);
    //       }
    //     }
    //   });
    // }// END for pos_i
    // // 结果,链有多少个出口:
    // END 取这个链的所有出口
    
    if(1 == arr_exit.length)
    {
      // 有效的落子:
      if(valid_moves[arr_exit[0][0]][arr_exit[0][1]])
      {
        let chain_data = {'chain_number':opponent_chain_number,'chain_length':all_pos_of_chain.length
          ,'exit_pos':arr_exit[0]};
        arr_opponent_dying_chain.push(chain_data);
      }
      else
      {
        ns.print("#### 无效落子");
      }
    }
    // ns.print("#### ",arr_exit);
  }// END for


  if(0==arr_opponent_dying_chain.length)
  {
    return [];
  }
  
  // 3. 取能吃的最大的链
  let max_length = 0;// 最大的链的长度
  let obj_target_chain=undefined;
  for(let i = 0;i<arr_opponent_dying_chain.length;++i)
  {
    let obj_data = arr_opponent_dying_chain[i];
    if(obj_data.chain_length > max_length)
    {
      max_length = obj_data.chain_length ;
      obj_target_chain = obj_data;
    }
  }// END for 取最大的串
  if(typeof(obj_target_chain) == 'undefined')
  {
    ns.print("!!!!    90 undefined     eat.js");
  }

  return obj_target_chain.exit_pos;
}